import { reqgetbanner, reqgetnew, reqindexgoods, reqcate, reqgetgroupon, reqgetcountdown, reqgetseckill } from '../../request/api';
import { countTime } from '../../filters';

//状态
const initState = {
    // 1.1初始化数据 
    getnew: [],//新人专享
    getindexgoods: [],//首页商品信息
    groupon: [],//万人团
    seckill: [],//秒杀
    cates: [],//商品分类
    n: 0,
    banner: [],
    timeArr: { h: 0, m: 0, s: 0 }
}

//actionType
const TYPES = {
    // 1.2 修改getnew
    HOME_CHANGE_GETNEW: 'HOME_CHANGE_GETNEW',
    HOME_CHANGE_INDEXGOODS: 'HOME_CHANGE_INDEXGOODS',
    HOME_CHANGE_GROUPON: 'HOME_CHANGE_GROUPON',
    HOME_CHANGE_SECKILL: 'HOME_CHANGE_SECKILL',
    HOME_CHANGE_CATES: 'HOME_CHANGE_CATES',
    HOME_CHANGE_N: 'HOME_CHANGE_N',
    HOME_CHANGE_BANNER: 'HOME_CHANGE_BANNER',
    HOME_CHANGE_TIMEARR: 'HOME_CHANGE_TIMEARR',

}

//修改state reducer 
const reducer = (state = initState, action) => {
    switch (action.type) {
        // 1.3修改getnew
        case TYPES.HOME_CHANGE_GETNEW:
            return {
                ...state,
                getnew: action.getnew
            }
        case TYPES.HOME_CHANGE_INDEXGOODS:
            return {
                ...state,
                getindexgoods: action.getindexgoods
            }
        case TYPES.HOME_CHANGE_GROUPON:
            return {
                ...state,
                groupon: action.groupon
            }
        case TYPES.HOME_CHANGE_SECKILL:
            return {
                ...state,
                seckill: action.seckill
            }
        case TYPES.HOME_CHANGE_CATES:
            return {
                ...state,
                cates: action.cates
            }
        case TYPES.HOME_CHANGE_N:
            return {
                ...state,
                n: action.n
            }
        case TYPES.HOME_CHANGE_BANNER:
            return {
                ...state,
                banner: action.banner
            }
        case TYPES.HOME_CHANGE_TIMEARR:
            return {
                ...state,
                timeArr: action.timeArr
            }
        default:
            return state;
    }
}

//做逻辑和异步的acitons actionCreators
let timer = null;
export const actions = {
    // 1.4 修改getnew的action
    changeGetnew: getnew => ({ type: TYPES.HOME_CHANGE_GETNEW, getnew }),
    requestGetnew: () => {
        return (dispatch, getState) => {
            let newp = getNew(getState())
            if (newp.length > 0) {
                return;
            }
            reqgetnew().then(res => {
                dispatch(actions.changeGetnew(res.data.list))
            })
        }
    },

    changeIndexgoods: getindexgoods => ({ type: TYPES.HOME_CHANGE_INDEXGOODS, getindexgoods }),
    requestIndexgoods: () => {
        return (dispatch, getState) => {
            let igoods = getIndexgoods(getState())
            if (igoods.length > 0) {
                return;
            }
            reqindexgoods().then(res => {//首页商品
                dispatch(actions.changeIndexgoods(res.data.list))
            })
        }
    },
    changeGroupon: groupon => ({ type: TYPES.HOME_CHANGE_GROUPON, groupon }),
    requestGroupon: () => {
        return (dispatch, getState) => {
            let gpon = getGroupon(getState())
            if (gpon.length > 0) {
                return;
            }
            reqgetgroupon().then(res => { //万人团
                dispatch(actions.changeGroupon(res.data.list))
            })
        }
    },
    changeSeckill: seckill => ({ type: TYPES.HOME_CHANGE_SECKILL, seckill }),
    requestSeckill: () => {
        return (dispatch, getState) => {
            let kill = getSeckill(getState())
            if (kill.length > 0) {
                return;
            }
            reqgetseckill().then(res => { //秒杀
                dispatch(actions.changeSeckill(res.data.list))
            })
        }
    },
    changeCates: cates => ({ type: TYPES.HOME_CHANGE_CATES, cates }),
    requestCates: () => {
        return (dispatch, getState) => {
            let cate = getCates(getState())
            if (cate.length > 0) {
                return;
            }
            reqcate().then(res => { //商品分类
                dispatch(actions.changeCates(res.data.list))
            })
        }
    },
    changeN:n=> ({type:TYPES.HOME_CHANGE_N, n}),
    changeBanner: banner => ({ type: TYPES.HOME_CHANGE_BANNER, banner }),
    requestBanner: () => {
        return (dispatch, getState) => {
            let banner = getBanner(getState())
            if (banner.length > 0) {
                return;
            }
            reqgetbanner().then(res => { //轮播图
                dispatch(actions.changeBanner(res.data.list))
            })
        }
    },
    changeTimearr: timeArr => ({ type: TYPES.HOME_CHANGE_TIMEARR, timeArr }),
    requestTimearr: () => {
        return (dispatch, getState) => { 
            timer = setInterval(() => {
                let times = countTime("2022-05-29 00:00:00");
                if(times == 0){
                    times = { h: 0, m: 0, s: 0 }
                }
                dispatch(actions.changeTimearr(times))
            })
        }
    },
    reqdel:()=> {
        return (dispatch, getState) => {
            clearInterval(timer);
        }
    }
}


//导出数据 Selectors 
export const getNew = (state) => state.home.getnew;

export const getIndexgoods = (state) => state.home.getindexgoods;

// 3.7 展示到页面的数据 
export const showIndexgoods = state => {
    let goods = getIndexgoods(state);
    let n = getN(state)
    return goods.length === 0 ? [] : goods[n].content
}

export const getGroupon = (state) => state.home.groupon;

export const getSeckill = (state) => state.home.seckill;

export const getCates = (state) => state.home.cates;

export const getN = state => state.home.n;

export const getBanner = (state) => state.home.banner;

export const getTimeArr = (state) => state.home.timeArr;

//导出reducer 
export default reducer;